package com.ciwong.epaper.modules.evaluate.ui;

import android.content.Intent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.evaluate.bean.ShareBean;
import com.ciwong.epaper.modules.me.util.AddPointTypes;
import com.ciwong.epaper.modules.share.QQShareHelper;
import com.ciwong.epaper.modules.share.WxShareHelper;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.widget.NewLoginDialog;
import com.ciwong.epaper.widget.ShareDialog;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.google.gson.Gson;
import com.tencent.connect.common.Constants;

/**
 * 排行榜
 * Created by lzr on 2017/6/9.
 */
public class RankingActivity extends BaseHtmlActicity {

    private String clientId;

    private String contentId;
    private ImageButton rankShareButton;
    private ImageButton rankBackButton;
    private RelativeLayout rankTitleContainer;

    @Override
    protected void init() {
        super.init();
        setValideSource(false);
//		setTitleText("排行榜");

        hideTitleBar();
        rankShareButton = (ImageButton) findViewById(R.id.rank_share);
        rankBackButton = (ImageButton) findViewById(R.id.rank_back);
        rankTitleContainer = (RelativeLayout) findViewById(R.id.rll_rank_title);
        rankTitleContainer.setVisibility(View.VISIBLE);
    }


    @Override
    protected void initEvent() {
        super.initEvent();
        EventBus.getDefault().register(this);
    }

    private String title;

    @Override
    protected void loadData() {
        super.loadData();
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {
            clientId = verifyInfo.getClientId();
        }
        Intent intent = getIntent();
        contentId = intent.getStringExtra(IntentFlag.INTENT_FLAG_CONTENTID);
        title = intent.getStringExtra(IntentFlag.INTENT_FLAG_TITLE);

        String userId = EApplication.getInstance().getUserInfoBase().getUserId() + "";
        String url = EpaperConstant.URL_RANKING_H5 + "contentId=" + contentId + "&clientId=" + clientId + "&userId=" + userId + "&brandId=" + EApplication.BRAND_ID + "&pageType=1";
        setStartURL(url);
        CWLog.i("排行榜url:", startURL.toString());
        loadUrl(startURL.toString());


        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                setResult(RESULT_OK);
                finish();
            }
        });
    }

    public void onEventMainThread(SubmitEvent submitEvent) {
        if (submitEvent.getId().equals("GetShareInfo")) { //标题栏显示分享按钮
            showShareBtn(submitEvent);
        }
    }


    @Override
    public void onBackPressed() {
        setResult(RESULT_OK);
        finish();
    }

    Gson gson = new Gson();

    @Override
    protected void onResume() {
        super.onResume();
        WxShareHelper.getInstance().onResumeResultData();
        rankBackButton.setOnClickListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void showShareBtn(final SubmitEvent submitEvent) {
        rankShareButton.setVisibility(View.VISIBLE);
        rankShareButton.setOnClickListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View v) {
                try {
                    if (EConstants.IS_YOUKE) {
                        NewLoginDialog newLoginDialog = new NewLoginDialog(RankingActivity.this);
                        newLoginDialog.showLoginDialog();

                    } else {
                        ShareBean shareBean = gson.fromJson(submitEvent.getJson(), ShareBean.class);
                        shareBean.moduleId = -1;
                        shareBean.setTitle(title);
                        ShareDialog shareDialog = new ShareDialog(RankingActivity.this, shareBean);
                        shareDialog.setParamForAddPoint(contentId, AddPointTypes.SHERE_RANK);//添加分享加积分

//                        toastInCenter("doworkID:"+shareBean.getDoWorkId());
//                        System.out.println("doworkID:"+shareBean.getDoWorkId());
                        shareDialog.show();
                    }
                } catch (Exception e) {
                    e.getStackTrace();
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constants.REQUEST_QQ_SHARE || requestCode == Constants.REQUEST_QZONE_SHARE) {
            QQShareHelper.getInstance().onActivityResultData(requestCode, resultCode, data);
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
